package org.su.generalmvc.config;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import org.su.generalmvc.scanner.XMLConfigLoader;
import org.apache.commons.lang.StringUtils;

import java.util.List;

public class XSSHConfig {

	

	@XStreamOmitField
	@XStreamAlias("result-types")
	private ResultTypes resultTypes;
	

	@XStreamImplicit(itemFieldName="package")
	private List<PackageConfig> packages;
	
	
	@XStreamOmitField
	@XStreamAlias("uploadConfig")
	private UploadConfig uploadConfig;
	
	/**
	 * 集群中标示服务器编号
	 */
	@XStreamOmitField
	private int serverId = 1;

	/**
	 * 编码设置
	 */
	@XStreamOmitField
	private String encoding = "utf-8";
	
	/**
	 * 指定velocity引擎目录
	 */
	@XStreamOmitField
	private String velocityPath;
	
	/**
	 * 指定系统是否使用xssh默认的数据库访问工具
	 */
	@XStreamOmitField
	private boolean useDB = false;
	
	/**
	 * 指定xssh是否使用spring管理bean，fasle：不需要
	 */
	@XStreamOmitField
	private boolean useSpring = true;
	
	
	/**
	 * 系统action默认后缀
	 */
	@XStreamOmitField
	@XStreamAlias("action-suffix")
	private String actionSuffix = ".do";
	
	/**
	 * 扫描起始包
	 */
	@XStreamOmitField
	@XStreamAlias("scanner-package")
	private String scannerPackage = "";
	
	/**
	 * 预处理器，这些预处理器是在系统加载的时候就执行，比contorller的init方法更早
	 */
	@XStreamOmitField
	@XStreamImplicit(itemFieldName="preprocessor")
	private List<BeanMapping> preprocessors;
	
	/**
	 * xssh的配置可以分散到多个文件中，但需要通过include包含到xssh.xml文件中
	 */
	@XStreamImplicit(itemFieldName="include")
	private List<String> includes;
	
	/**
	 * 每个action扫描后要执行的代码
	 */
	@XStreamOmitField
	@XStreamAlias("afterAcoinScaned")
	private String afterAcoinScaned;

    /**
     * 方法参数上的绑定器
     */
    @XStreamOmitField
    @XStreamAlias("paramBinder")
    private String paramBinder;

    /**
     * 系统action默认后缀
     */
    @XStreamOmitField
    @XStreamAlias("context-prefix")
    private String contextPrefix = "";
	
	
	public static String CLASS_PATH = "classpath:";
	
	public static String getConfigPath(String configPath,boolean defaultConfig){
		if(StringUtils.isNotEmpty(configPath)){
			if(configPath.startsWith(CLASS_PATH)){
				configPath = configPath.substring(10);
			}else if(configPath.startsWith("webClasspath:")){
                configPath = "WEB-INF/classes/"+configPath.substring(13);
            }
			
		}else if(defaultConfig){
			
			configPath = XMLConfigLoader.DEFAULT_XML_CONFIG;
		}
		return configPath;
	}
	
	public void copy(XSSHConfig config) {
		
		if(StringUtils.isNotBlank(config.actionSuffix)){
			this.actionSuffix = config.actionSuffix;
		}
		
		if(StringUtils.isNotBlank(config.scannerPackage)){
			this.scannerPackage = config.scannerPackage;
		}
		
		if(StringUtils.isNotBlank(config.getEncoding())){
			this.encoding = config.getEncoding();
		}
		
		if(config.getPackages() != null){
			if(this.packages != null){
				this.packages.addAll(config.getPackages());
			}else{
				this.packages = config.getPackages();
			}
		}
		
		if(config.getPreprocessors() != null){
			if(this.preprocessors != null){
			this.preprocessors.addAll(config.getPreprocessors());
			}else{
				this.preprocessors = config.getPreprocessors();
			}
		}
		
		if(config.getResultTypes() != null){
			this.resultTypes = config.getResultTypes();
		}
		
		if(config.getServerId() != 0){
			this.serverId = config.getServerId();
		}
		
		if(config.getUploadConfig() != null){
			this.uploadConfig = config.getUploadConfig();
		}
		
		
		if(StringUtils.isNotBlank(config.getVelocityPath())){
			this.velocityPath = config.getVelocityPath();
		}
	}
	
	public List<String> getIncludes() {
		return includes;
	}

	public List<BeanMapping> getPreprocessors() {
		return preprocessors;
	}

	public String getScannerPackage() {
		return scannerPackage;
	}

	public int getServerId() {
		return serverId;
	}
	
	public String getActionSuffix() {
		return actionSuffix;
	}

	public ResultTypes getResultTypes() {
		return resultTypes;
	}
	
	public List<PackageConfig> getPackages() {
		return packages;
	}
	
	public boolean isUseSpring() {
		return useSpring;
	}

	public String getVelocityPath() {
		return velocityPath;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public UploadConfig getUploadConfig() {
		return uploadConfig;
	}

	/**
	 * @return the isDAO
	 */
	public boolean isUseDB() {
		return useDB;
	}

	public String getAfterAcoinScaned() {
		return afterAcoinScaned;
	}

    public String getParamBinder(){
        return paramBinder;
    }

    public String getContextPrefix(){
        return contextPrefix;
    }
	
}
